package com.zlt.appparking.controller;


import com.zlt.appparking.dto.AppointmentDTO;
import com.zlt.appparking.service.AppointmentService;
import com.zlt.appparking.vo.AppointmentVO;
import com.zlt.appparking.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("appointments")
@Slf4j
public class AppointmentController {

    @Autowired
    private AppointmentService appointmentService;


    /**
     * 预订单的添加
     *
     * @param appointmentDTO
     * @return
     */
    @PostMapping()
    public Result add(@RequestBody AppointmentDTO appointmentDTO) throws ParseException {


        try {
            boolean success = appointmentService.insertAppointment(appointmentDTO);
            return success ? Result.ok("操作成功") : Result.error("操作失败");
        } catch (RuntimeException e) {
            return Result.error("失败");
        }
    }


    /**
     * 查询用户的所有预约
     *
     * @return
     */
    @GetMapping
    public Result list() {
        try {
            List<AppointmentVO> appointmentVOList = appointmentService.queryList();
            return Result.ok("操作成功", appointmentVOList);
        } catch (RuntimeException e) {
            log.debug(e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 取消预约订单
     *
     * @param apId
     * @return
     */
    @GetMapping("{apId}")
    public Result cancel(@PathVariable("apId") Integer apId) {
        try {
            boolean success = appointmentService.cancelReserve(apId);
            return success ? Result.ok("操作成功") : Result.error("操作失败");
        } catch (RuntimeException e) {
            log.debug(e.getMessage());
            return Result.error(e.getMessage());
        }
    }


}
